package com.baosight.sczx.zm.ck.service;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.ck.domain.ZMCKFH;

/**
 * 
 * <p>Title: ServiceZMCK31.java</p>
 * <p>Description: 防洪设施检查管理 </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 郭淑芳
 * @version 1.0
 * <pre>Histroy:
 *          2016年9月14日 上午10:23:39  郭淑芳  Create
 *</pre>
 */
public class ServiceZMCK31 extends ServiceEPBase {

	/**
	 * 
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化方法</p>
	 * <p>return value: ServiceZMCK31</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月14日上午10:23:54 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的

		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/**
	 * 
	 * <p>Function: query</p>
	 * <p>Description: 查询方法</p>
	 * <p>return value: ServiceZMCK31</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月14日上午10:24:10 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {	
		// 供雨季三防汇总页面使用
		String flag = inInfo.getString("flag");
		if (null != flag && !("").equals(flag)) {
			StaticTools.setQueryParameters(inInfo, "fdStatus", "上报", true);// 添加查询条件
		} else {
			StaticTools.setQueryParameters(inInfo, "fdDepartmentId", UserSession.getInSessionProperty("miningCode"), true);
		}
		String fdDepartmentId = inInfo.getString("departmentId");
		if (null != fdDepartmentId && !("").equals(fdDepartmentId)) {
			StaticTools.setQueryParameters(inInfo, "fdDepartmentId", fdDepartmentId, true);
		}
		
		String nf = inInfo.getString("nf");
		if (null != nf && !("").equals(nf)) {
			String fdBeginDate = nf + "-01-01";
			String fdEndDate = nf + "-12-31";
			StaticTools.setQueryParameters(inInfo, "fdBeginDate", fdBeginDate, true);// 添加查询条件
			StaticTools.setQueryParameters(inInfo, "fdEndDate", fdEndDate, true);// 添加查询条件
		}

		EiInfo outInfo = super.query(inInfo, "ZMCKFH.query", new ZMCKFH());
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.ck.fhssName", false));// 防洪设施名称
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.ck.fhssGm", false));// 防洪设施工程规模
		outInfo.set("fdDepartmentId", UserSession.getInSessionProperty("miningCode"));// 矿ID的默认值
		outInfo.set("fdDepartment", UserSession.getInSessionProperty("miningName"));// 矿名称的默认值

		return outInfo;
	}

	/**
	 * 
	 * <p>Function: insert</p>
	 * <p>Description: 新增方法</p>
	 * <p>return value: ServiceZMCK31</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月14日上午10:25:01 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCKFH _dom = new ZMCKFH();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdCurrentDate(StaticTools.getDate());
			_dom.setFdDepartmentId(UserSession.getInSessionProperty("miningCode").toString());
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CK_FH"));
			_dom.setFdDocid(StaticTools.getDocId());
			this.dao.insert("ZMCKFH.insert", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}

	/**
	 * 
	 * <p>Function: update</p>
	 * <p>Description: 修改方法</p>
	 * <p>return value: ServiceZMCK31</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月14日上午10:26:41 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {

		EiInfo out = new EiInfo();
		ZMCKFH _dom = new ZMCKFH();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdCurrentDate(StaticTools.getDate());
			this.dao.update("ZMCKFH.update", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}

	/**
	 * 
	 * <p>Function: delete</p>
	 * <p>Description: 删除方法</p>
	 * <p>return value: ServiceZMCK31</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月14日上午10:28:27 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCKFH.delete");

		EiInfo out = new EiInfo();
		out = this.query(inInfo);
		out.setStatus(_temp.getStatus());
		out.setMsg(_temp.getMsg());

		return out;
	}

	/**
	 * 
	 * <p>Function: report</p>
	 * <p>Description: 上报方法</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月14日上午10:28:07 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo) {
		EiInfo _temp = super.update(inInfo, "ZMCKFH.report");

		EiInfo out = new EiInfo();
		out = this.query(inInfo);
		out.setStatus(_temp.getStatus());
		out.setMsg(_temp.getMsg());

		return out;
	}
}
